package com.wrupple.muba.desktop.server.integration;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.wrupple.muba.desktop.domain.exceptions.NoDesktopViewResolvedException;

@Controller
public class DesktopController {
	protected final Log logger = LogFactory.getLog(getClass());
	private String viewName;
	private String desktopPageTitle;

	@RequestMapping(value = "/index.html", method = RequestMethod.GET)
	public ModelAndView accessGranted(Locale locale, HttpServletRequest req, HttpServletResponse resp)
			throws NoDesktopViewResolvedException {
		logger.debug("dektop requested");
		if (viewName == null ) {
			throw new NoDesktopViewResolvedException(
					"Unable to find a suitable sequentialViewWrapper to render the desktop in, no default sequentialViewWrapper Name is set and no processing filters could be found");
		}
		logger.debug("dispatching desktop request");
		ModelAndView regreso = new ModelAndView();
		logger.debug("Default desktop sequentialViewWrapper is " + viewName);
		regreso.setViewName(viewName);
		regreso.addObject("desktopTitle", desktopPageTitle);
		regreso.addObject("locale", locale.getLanguage());
		return regreso;
	}

	public void setViewName(String viewName) {
		this.viewName = viewName;
	}

	/**
	 * @param desktopPageTitle
	 *            the desktopPageTitle to set
	 */
	public void setDesktopPageTitle(String desktopPageTitle) {
		this.desktopPageTitle = desktopPageTitle;
	}

	/**
	 * @return the desktopPageTitle
	 */
	public String getDesktopPageTitle() {
		return desktopPageTitle;
	}

}
